package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.adapter.MaterialPosterAdapter;
import com.sanpu.kxe.business.main.bean.MaterialPosterOutput;
import com.sanpu.kxe.business.main.contract.MaterialPosterContract;
import com.sanpu.kxe.business.main.presenter.MaterialPosterPresenter;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.widget.CenterScrollListener;
import com.sanpu.kxe.module.widget.OverFlyingLayoutManager;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：银信通推广海报
 * Created by 薛勇军 on 2019/9/27.
 */

public class MaterialPosterActivity extends BaseTitleActivity implements MaterialPosterContract.View {

    @Bind(R.id.chooseImageIv)
    ImageView chooseImageIv;
    @Bind(R.id.bannerRv)
    RecyclerView bannerRv;

    private OverFlyingLayoutManager mOverFlyingLayoutManager;

    private MaterialPosterAdapter vPosterAdapter;

    private MaterialPosterContract.Presenter mPresenter;

    private String mChooseImagePath;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_material_poster;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("推广海报");

        bannerRv.addOnScrollListener(new CenterScrollListener());
        mOverFlyingLayoutManager = new OverFlyingLayoutManager(1f, 0, 0, OverFlyingLayoutManager.HORIZONTAL);
        vPosterAdapter = new MaterialPosterAdapter(mContext);
        bannerRv.setAdapter(vPosterAdapter);
        bannerRv.setLayoutManager(mOverFlyingLayoutManager);
        mOverFlyingLayoutManager.setOnPageChangeListener(new OverFlyingLayoutManager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                MaterialPosterOutput posterOutput = vPosterAdapter.getItem(position);
                if (posterOutput != null) {
                    mChooseImagePath = UrlConstants.getBaseUrl() + posterOutput.getBigImagePath();
                    JKImageLoader.getInstance().displayImage(mContext,
                            UrlConstants.getBaseUrl() + posterOutput.getBigImagePath(), chooseImageIv);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mPresenter = new MaterialPosterPresenter(this);
        mPresenter.promotionPoster();
    }

    @Override
    public void showPosterListView(List<MaterialPosterOutput> posterOutputs) {
        if (posterOutputs != null && posterOutputs.size()>0) {
            bannerRv.setVisibility(View.VISIBLE);
            int chooseIndex = 0;
            if (posterOutputs.size()>1) {
                mOverFlyingLayoutManager.scrollToPosition(1);
                chooseIndex = 1;
            }
            vPosterAdapter.setNotifyData(posterOutputs);
            mChooseImagePath = UrlConstants.getBaseUrl() + posterOutputs.get(chooseIndex).getBigImagePath();
            JKImageLoader.getInstance().displayImage(mContext,
                    UrlConstants.getBaseUrl() + posterOutputs.get(chooseIndex).getBigImagePath(), chooseImageIv);
        } else {
            bannerRv.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.shareWeChatLl, R.id.shareTimelineLl, R.id.saveSystemLl})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shareWeChatLl:
                chooseImageIv.destroyDrawingCache();
                chooseImageIv.setDrawingCacheEnabled(true);
                Bitmap mShareWeChat = chooseImageIv.getDrawingCache();
                mPresenter.onShareWeChatClick(mShareWeChat);
                break;
            case R.id.shareTimelineLl:
                chooseImageIv.destroyDrawingCache();
                chooseImageIv.setDrawingCacheEnabled(true);
                Bitmap mShareTimeline = chooseImageIv.getDrawingCache();
                mPresenter.onShareTimelineClick(mShareTimeline);
                break;
            case R.id.saveSystemLl:
                chooseImageIv.destroyDrawingCache();
                chooseImageIv.setDrawingCacheEnabled(true);
                Bitmap mClipScreen = chooseImageIv.getDrawingCache();
                mPresenter.onSaveSystemClick(mClipScreen);
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MaterialPosterContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
